EFFECT.Exp = {}
EFFECT.Exp[1] = Sound("ambient/explosions/explode_4.wav")
EFFECT.Exp[2] = Sound("ambient/explosions/explode_9.wav")

function EFFECT:Init(data)
	
	self.Prop = data:GetEntity()
	if self.Prop == NULL then self.Entity:Remove() end
	self.Scale = data:GetMagnitude()
	
	self.LifeTime = 10
	self.GenTime = CurTime()
	
	self.MaxTries = 10
	
	self.Size = 1
	
	self.valid = false
	if self.Prop == NULL or not self.Prop:IsValid() then return end
	local tracelength = self.Prop:OBBMaxs():Length()
	
	for i=1,self.MaxTries do
		if not self.valid then
			local Center = self.Entity:RndPropOrigin(self.Prop)
			local Origin = Center + (self.Entity:RndNormalVector() * tracelength)
			local tracedata = {}
			tracedata.start = self.Prop:LocalToWorld(Origin)
			tracedata.endpos = self.Prop:LocalToWorld(Center)
			local trace = util.TraceLine(tracedata)
			if trace.Hit and trace.Entity:IsValid() and (trace.Entity == self.Prop) then
				self.valid = true
				self.Origin = trace.HitPos
				self.Normal = trace.HitNormal
				util.Decal("Scorch", self.Origin + self.Normal, self.Origin - self.Normal)
			end
		end
	end
	
	self.Mat = {}
	
	self.Mat.Flare = Material("Sprites/glow01")
	self.Mat.Flare:SetMaterialInt("$spriterendermode",5)
	self.Mat.Flare:SetMaterialInt("$illumfactor",8)
	
	self.Mat.Core = Material("Sprites/light_glow01")
	self.Mat.Core:SetMaterialInt("$spriterendermode",5)
	self.Mat.Core:SetMaterialInt("$illumfactor",8)
	
	self.particles = {}

	if self.valid then
	
	self.Entity:SetPos(self.Origin)
	self.Entity:SetParent(self.Prop)
	
	WorldSound(self.Exp[math.random(1,2)], self.Origin, 100, 100)
	--self.Velocity = self.Prop:GetPhysicsObject():GetVelocity()
	
		local emitter = ParticleEmitter(self.Entity:GetPos())
		for i=1,20 do
			local particle = emitter:Add("Sprites/glow01", self.Entity:GetPos())
			particle:SetColor(255, 140, 0, 255)
			particle:SetVelocity((self.Normal * 50) + (self.Entity:RndNormalVector() * 40))
			particle:SetDieTime(self.LifeTime * 2)
			particle:SetLifeTime(math.Rand(0, self.LifeTime))
			particle:SetStartSize(math.Rand(0, self.Scale/2))
			particle:SetEndSize(0)
			particle:SetStartAlpha(255)
			particle:SetEndAlpha(0)
		end
		
		local detonator = ParticleEmitter(self.Entity:GetPos())
		for i=1,5 do
			local particle = detonator:Add("particles/flamelet" .. math.random(1,5), self.Entity:GetPos())
			particle:SetColor(255, 140, 0, 255)
			particle:SetVelocity(Vector(0, 0, 0))
			particle:SetDieTime(self.LifeTime)
			particle:SetLifeTime(0)
			particle:SetStartSize(math.Rand(self.Scale / 2, self.Scale))
			particle:SetEndSize(self.Scale)
			particle:SetStartAlpha(255)
			particle:SetEndAlpha(0)
			table.insert(self.particles, particle)
		end
	end
	
end

function EFFECT:RndNormalVector()
	local Vect = Vector()
	Vect.x = math.Rand(-1,1)
	Vect.y = math.Rand(-1,1)
	Vect.z = math.Rand(-1,1)
	return Vect
end

function EFFECT:RndPropOrigin(entity)
	local Vect = Vector()
	local OBB = entity:OBBMaxs()
	Vect.x = math.Rand(-1,1) * OBB.x
	Vect.y = math.Rand(-1,1) * OBB.y
	Vect.z = math.Rand(-1,1) * OBB.z
	return Vect
end

function EFFECT:Think()
	if ((self.GenTime + self.LifeTime) < CurTime()) or not self.valid or not self.Prop:IsValid() then
		for _,particle in pairs(self.particles) do
			particle:SetDieTime(0)
		end
		return false
	else
		self.Size = math.Clamp(1 - ((CurTime() - self.GenTime) / self.LifeTime), 0, 1)
		return true
	end
end

function EFFECT:Render()
	
	--Lens Flare Effect
	render.SetMaterial(self.Mat.Flare)
	render.DrawSprite(self.Entity:GetPos(), self.Size * self.Scale * 40, self.Size * self.Scale, Color(0, 0, 255, self.Size * 100))
	
	--Core Light Effect
	render.SetMaterial(self.Mat.Core)
	render.DrawSprite(self.Entity:GetPos(), self.Size * self.Scale * 8, self.Size * self.Scale * 8, Color(255, 140, 0, self.Size * 255))
	
	--Explosion Particles
	for _,particle in pairs(self.particles) do
		particle:SetPos(self.Entity:GetPos())
	end
end
